<?php
/**
 * TomatoCMS
 *
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 *
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: list.phtml 4684 2010-08-16 08:44:14Z huuphuoc $
 * @since		2.0.0
 */
?>
<?php
$this->headTitle()->prepend($this->translate('user_manager') . ' - ');
$this->headScript()->appendFile($this->APP_STATIC_SERVER . '/public/js/jquery.json-2.3.min.js');
?>

<div id="r-heading">
    <img src="<?php echo $this->APP_STATIC_SERVER . '/skins/admin/default/images/user.png'; ?>" alt=""/>
    <span><?php echo $this->translate('user_list_page_title') ?></span>
</div>
<?php
//nếu có message thì hiển thị
$mesage = $this->flashMessenger();
if ($mesage) {
    echo '<div id="message">' . $mesage . '</div>';
}
?>
<div>
    <form id="frmActions" action="" method="post">
        <div id="actions">
              <!--<input type="text" id="txtKeyword" name="keyword" class="text" value="" size="25"/>
              <input type="submit" id="btnSearch" class="button" value="Tìm kiếm"/>  -->
            <input type="button" id="btnCreate" class="button" value="<?php echo $this->translate('user_add_breadcrumb') ?>" onclick="location = '<?php echo $this->url(array(), 'core_user_add'); ?>';"/>
            <input type="button" id="btnDelete" class="button" value="<?php echo $this->translate('user_action_delete_many') ?>"/>
        </div>
    </form>
    <table class="list" cellspacing="0">
        <thead>
            <tr>
                <td width="3%">
                    <input type="checkbox" class="chkAll"/>
                </td>
                <td width="20%">
                    <?php echo $this->translate('user_list_user_name'); ?>
                </td>
                <td width="10%">
                    <?php echo $this->translate('user_list_role'); ?>
                </td>
                <td width="25%">
                    <?php echo $this->translate('user_list_logged_in_date'); ?>
                </td>
                <td width="15%">
                    <?php echo $this->translate('user_list_status'); ?>
                </td>
                <td width="10%">
                    <?php echo $this->translate('user_list_online'); ?>
                </td>
                <td width="15%">
                    <?php echo $this->translate('user_list_actions'); ?>
                </td>
            </tr>
        </thead>
        <tbody>
            <?php
            if (count($this->users) == 0) {
            ?>
            <tr>
                <td colspan="7" style="color: #fc0202;">
                <?php echo $this->translate('user_list_not_found'); ?>
                </td>
            </tr>
            <?php
            } else {
                $count = count($this->users);
                $i = 0;
                foreach ($this->users as $user) {
            ?>
            <tr id="user_<?php echo $user->id ?>" <?php if ($i == $count - 1) echo 'class="last"'; ?>>
                <td>
                    <input type="checkbox" class="chkId" value = "<?php echo $user->id; ?>"/>
                </td>
                <td><?php echo $user->username ?></td>
                <td><?php echo $user->role_name; ?></td>
                <td><?php echo $user->logged_in_date;?></td>
                <td class="user-status"><?php echo $this->translate(($user->actived) ? 'user_list_status_actived' : 'user_list_status_inactived');?></td>
                <td><?php //echo $this->translate(($user->is_online) ? 'user_list_is_online' : 'user_list_not_online') ?></td>
                <td>
                <?php
                if ($this->allow('edit')) {
                ?>
                    <a href="<?php echo $this->url(array('id' => $user->id), 'core_user_edit'); ?>"><?php echo $this->translate('user_list_action_edit'); ?></a>
                <?php
                }
                if ($this->allow('activate')) {
                ?>
                | <a class="activateAction" rel="<?php echo $user->id.'_'.$user->actived ?>" href="javascript: void(0);">
                    <?php echo $this->translate(($user->actived) ? 'user_list_action_deactivate' : 'user_list_action_activate'); ?>
                </a>
                <?php
                }
                if ($this->allow('delete')) {
                ?>
                |  <a rel="<?php echo $user->id ?>" class="deleteAction" href="#" rel="<?php echo $user->id; ?>">
                        <?php echo $this->translate('user_list_action_delete') ?>
                    </a>
                <?php
                }
                ?>
                </td>
            </tr>
            <?php
                $i++;
                }
            } ?>
            </tbody>
            <tfoot>
                <tr>
                    <td>
                        <input type="checkbox" class="chkAll"/>
                    </td>
                    <td>
                        <?php echo $this->translate('user_list_user_name'); ?>
                    </td>
                    <td>
                        <?php echo $this->translate('user_list_role'); ?>
                    </td>
                    <td>
                        <?php echo $this->translate('user_list_logged_in_date'); ?>
                    </td>
                    <td>
                        <?php echo $this->translate('user_list_status'); ?>
                    </td>
                    <td>
                        <?php echo $this->translate('user_list_online'); ?>
                    </td>
                    <td>
                        <?php echo $this->translate('user_list_actions'); ?>
                    </td>
                </tr>
            </tfoot>
        </table>
</div>
<script type="text/javascript">
    $(document).ready(function(){
        <?php
        if($this->allow('activate')){
        ?>
        //activate Action
        $(".activateAction").click(function(){
            var self = this;
            var rels = $(this).attr("rel").split("_");
            $(this).parent().parent().find(".user-status").html("<img src='<?php echo $this->APP_STATIC_SERVER.'/skins/admin/default/images/loading.gif'?>' alt='' />");
            $.ajax({
                type: "POST",
                url: "<?php echo $this->url(array(),'core_user_activate');?>",
                data: { id: rels[0]},
                success: function(response){
                    if(response == "RESULT_OK"){
                        rels[1] = 1 - rels[1];
                        $(self).attr('rel', rels[0] + '_' + rels[1]);
                    }else if(response == "RESULT_DENY"){
                        alert("<?php echo $this->translate('user_list_action_activate_deny') ?>");
                    }else{
                        alert("<?php echo $this->translate('user_list_action_activate_error') ?>");
                    }
                    if (rels[1] == 1) {
                        $(self).html("<?php echo $this->translate('user_list_action_deactivate'); ?>");
                        $(self).parent().parent().find(".user-status").html("<?php echo $this->translate('user_list_status_actived'); ?>");
                    } else {
                        $(self).html("<?php echo $this->translate('user_list_action_activate'); ?>");
                        $(self).parent().parent().find(".user-status").html("<?php echo $this->translate('user_list_status_inactived'); ?>");
                    }
                }
            });

        });
        <?php
        }
        ?>

        <?php if($this->allow('delete')){
        ?>
        //delete Action
        $(".deleteAction").click(function(){
            if(confirm("<?php echo $this->translate('user_list_action_delete_confirm') ?>")){
                var id = $(this).attr("rel");
                $.ajax({
                    type: 'POST',
                    url: "<?php echo $this->url(array(), 'core_user_delete'); ?>",
                    data: { id: id },
                    success: function(response) {
                        var result = $.evalJSON(response);
                        if ("ok" == result.status) {
                            for(var i = 0; i< result.ids.length; i++)
                                $('#user_' + result.ids[i]).fadeOut('slow');
                        }else{
                            alert("<?php echo $this->translate('user_action_delete_error')?>")
                        }
                    }
                });
            }
            return false;
        });
        //xóa
        $("#btnDelete").click(function(){
            var arrChkId = $(".chkId");
            var ids = new Array();
            for(var i =0; i < arrChkId.length; i++){
                if(arrChkId.eq(i).attr("checked")){
                    ids.push(arrChkId.eq(i).val());
                }
            }
            if(ids.length > 0){
                if(confirm("<?php echo $this->translate('user_list_action_delete_many_confirm') ?>")){
                    $.ajax({
                        type: 'POST',
                        url: "<?php echo $this->url(array(), 'core_user_delete'); ?>",
                        data: { id: $.toJSON(ids) },
                        success: function(response) {
                            var result = $.evalJSON(response);
                            if ("ok" == result.status) {
                                for(var i = 0; i< result.ids.length; i++)
                                    $('#user_' + result.ids[i]).fadeOut('slow');
                            }else{
                                alert("<?php echo $this->translate('user_action_delete_error')?>")
                            }
                        }
                    });
                }else{
                    return false;
                }
            }else{
                alert("<?php echo $this->translate('user_list_bulk_action_none_selected') ?>");
                return false;
            }
        });
        <?php
        }
        ?>

    });
</script>